<?php
class Customer extends AppModel
{
    /**
     * associations
     *
     * @var array
     */

    var $hasMany = array(
        'CustomerReminder' => array(
            'className' => 'CustomerReminder',
            'foreignKey' => 'customer_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    var $belongsTo = array(
        'Region' => array(
            'className' => 'Region',
            'foreignKey' => 'region_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        )
    );


    function getCustomerById($Id = null, $recursive = -1)
    {
        $conditions = array(
            'Customer.id' => $Id,
            'Customer.deleted' => 'n',
        );
        # fetch the specific data from the server and retrun it.
        return $this->find('first', array('conditions' => $conditions, 'recursive' => $recursive));
    }

    function getCustomers($type = 'customer',$fields=array(),$contain=array(),$recursive = -1)
    {
        $conditions = array(
            'Customer.type' => $type,
            'Customer.deleted' => 'n'
        );
        $query_dt = array('conditions' => $conditions, 'recursive' => $recursive);
        if(!empty($fields)){
            $query_dt['fields']=$fields;
        }
        if(!empty($contain)){
            $query_dt['contain']=$contain;
            unset($query_dt['recursive']);
        }
        # fetch the specific data from the server and retrun it.
        return $this->find('all', $query_dt);
    }

    function getBirthdayCustomers($customer_type)
    {
        $month_day = date('m-d');
        $conditions = array(
            'Customer.type' => $customer_type,
            'Customer.deleted' => 'n',
        );
        $customers = $this->find('all', array('conditions' => $conditions, 'recursive' => -1));
        $birthday_today = array();
        foreach($customers as $customer){
            $dob = trim($customer['Customer']['dob']);
            $wished = $customer['Customer']['wished']; //We want those who have not been wished
            if(!empty($dob)){
                $month_day_dob = date('m-d',strtotime($dob));
                if($month_day == $month_day_dob && $wished == 'No'){
                    $birthday_today[]=$customer;
                }
                else{//Reset the wished for another time
                    if($month_day != $month_day_dob){
                        $this->save(array(
                            'id'=>$customer['Customer']['id'],
                            'wished'=>'No'
                        ));
                    }
                }
            }
        }
        # fetch the specific data from the server and retrun it.
        return $birthday_today;
    }


    function updateCustomerBirthdayWished($ids,$wish='Yes'){
        $result = $this->updateAll(
            array('Customer.wished' => "'$wish'"),
            array('Customer.id' => $ids)
        );
        return $result;
    }

}